<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$statuss=array(
	'0'=>'待上传代码',
	'1'=>'待提交审核',
	'2'=>'等待审核结果',
	'3'=>'待发布',
	'4'=>'发布成功'
);
$return['types']=array(
	'1'=>'微官网小程序',
	'2'=>'插件小程序',
	'7'=>'打包小程序',
	'8'=>'多页小程序',
);
if($_RQ['op']=='list'){
	//小程序列表
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	$fields=array('id','title','type','status');
	$pindex = max(1, $_RQ['page']);
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_baiduapp',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$row){
			$row['fans']=array('action'=>'baiduapp.fans','op'=>'list','query'=>'baiduapp_id='.$row['id'],'title'=>'粉丝管理');

		}
	}
	$return['statuss']=$statuss;
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'小程序列表'),
			array('op'=>'add','title'=>'创建小程序'),
		),
		'list'=>array('data'=>'list','tip'=>'温馨提示，列表“状态”一栏仅指通过微信开放平台第三方授权上传的上传状态。打包小程序和diy小程序的上传状态请前往上传任务列表查看！','list'=>array(
			array('type'=>'string','title'=>'小程序名称','name'=>'title'),
			array('type'=>'radio','title'=>'小程序类型','name'=>'type','options'=>'types'),

			//array('type'=>'string','title'=>'访问量/占比','name'=>'fangwen'),
			//array('type'=>'href','title'=>'粉丝管理','name'=>'fans'),
			//array('type'=>'href','title'=>'支付参数设置','op'=>'pay'),
		),'action'=>array(
			array('type'=>'post','op'=>'update','title'=>'更新','show'=>array('update',1)),
			//array('type'=>'href','op'=>'up','title'=>'生成新版本','show'=>array('status'=>0,'type'=>2)),
			array('type'=>'href','op'=>'get_ewm','action'=>'baiduapp.up','title'=>'生成新版本'),
			array('type'=>'href','op'=>'list','action'=>'baiduapp.up_list','title'=>'版本列表'),
			array('type'=>'href','op'=>'template','title'=>'保存到我的模版','show'=>array('type',1)),
			array('type'=>'href','op'=>'data','title'=>'关联插件','show'=>array('type',2)),
			array('type'=>'href','op'=>'design','title'=>'设计','show'=>array('type',array(1,3))),
		)),
		'search_one'=>array('name'=>'title','add_op'=>'add'),
	);

	$return['htmls']=$htmls;
	set_html('header','common/header');
	set_html('title','小程序');
	exi($return);
}
if($_RQ['op']=='add'){
	//添加小程序
	model('core/web')->check_num('baiduapp');
	if($_RQ['postdata']){
		$post=get_postdata();
		$shownums=array(
			'news_num'=>intval($post['news_num']),
			'stores_num'=>intval($post['stores_num']),
			'products_num'=>intval($post['products_num']),
			'anlis_num'=>intval($post['anlis_num']),
		);
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'type'=>intval($post['type']),
			'is_wz'=>intval($post['is_wz']),
			'title'=>$post['title'],
			'desc'=>$post['desc'],
			'logo'=>$post['logo'],
			'textcolor'=>$post['textcolor'],
			'bgcolor'=>$post['bgcolor'],
			'appid'=>$post['appid'],
			'appsecret'=>$post['appsecret'],
			'oldid'=>$post['oldid'],
			'url'=>$post['url'],
			'createtime'=>TIMESTAMP,
			'shownums'=>ijson_encode($postdata['shownums']),
		);
		pdo_insert('core_account_baiduapp',$insert);
		$id=pdo_insertid();
		if($insert['type']==2){
			exi('添加成功！请选择小程序模版！','',array('op'=>'list','action'=>'baiduapp.plugin','query'=>'id='.$id));
		}
		exi('添加成功！','',array('op'=>'list'));
	}else{
		$return['item']=array();
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'设置小程序类型',
			'tabs'=>array(
				array('op'=>'list','title'=>'返回小程序列表'),
			),
			'edit'=>array(
				array('type'=>'string','name'=>'title','title'=>'小程序名称','help'=>'小程序首页分享标题'),
				array('type'=>'string','name'=>'desc','title'=>'小程序描述','help'=>'小程序首页分享描述'),
				array('type'=>'image','name'=>'logo','title'=>'小程序LOGO','help'=>'小程序首页分享图片'),
				array('type'=>'color','name'=>'textcolor','title'=>'小程序标题颜色'),
				array('type'=>'color','name'=>'bgcolor','title'=>'小程序标题背景色','help'=>'小程序首页分享图片'),
				array('type'=>'switch','name'=>'is_wz','title'=>'第三方小程序？','help'=>'如果是第三方小程序：1、必须先设置好第三方域名；1、从第三方登陆！'),
				array('type'=>'string','name'=>'news_num','title'=>'新闻个数','help'=>'显示多少条新闻?','show'=>array('is_wz',0)),
				array('type'=>'string','name'=>'news_num','title'=>'门店个数','help'=>'显示多少个门店?','show'=>array('is_wz',0)),
				array('type'=>'string','name'=>'news_num','title'=>'产品个数','help'=>'显示多少个产品?','show'=>array('is_wz',0)),
				array('type'=>'string','name'=>'news_num','title'=>'案例个数','help'=>'显示多少个案例?','show'=>array('is_wz',0)),
				array('type'=>'radio','name'=>'type','title'=>'小程序类型','options'=>'types','help'=>'已经完善所有组件！尽情DIY吧!'),
				array('type'=>'string','title'=>'appid','name'=>'appid','help'=>'填写小程序appid' ),
				array('type'=>'string','title'=>'appsecret','name'=>'appsecret','help'=>'填写小程序App secret'),
				array('type'=>'link','name'=>'url','title'=>'要打包的页面链接','help'=>'如果是打包功能插件，请选择功能插件的入口地址','show'=>array('type',7)),
			),
		);
		exi($return);
	}
}

if($_RQ['op']=='pay'){
	//设置小程序支付参数
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择要设置的小程序！','error');
		}
		$data=array(
			'mchid'=>$post['mchid'],
			'paykey'=>$post['paykey'],
		);
		pdo_update('core_account_baiduapp',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！');
	}else{
		$id=intval($_RQ['id']);
		if(!$id){
			exi('请先选择要设置的小程序！','error');
		}
		$item=pdo_get('core_account_baiduapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title','mchid','paykey'));
		$return['item']=$item;
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'设置小程序支付参数',
			'tabs'=>array(
				array('op'=>'list','title'=>'小程序列表'),
			),
			'edit'=>array(
				array('type'=>'string','name'=>'title','title'=>'小程序名称','read'=>true),
				array('type'=>'string','name'=>'mchid','title'=>'微信支付商户号','help'=>'微信支付商户号，从微信商户平台获取（开通微信支付的通知邮件中也有）'),
				array('type'=>'string','name'=>'paykey','title'=>'微信支付密钥','help'=>'设置微信支付密钥，需要和微信商户平台的api密钥保持一致'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='edit'){
	//设置小程序基本信息
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择要设置的小程序！','error');
		}
		$shownums=array(
			'news_num'=>intval($post['news_num']),
			'stores_num'=>intval($post['stores_num']),
			'products_num'=>intval($post['products_num']),
			'anlis_num'=>intval($post['anlis_num']),
		);
		$data=array(
			'title'=>$post['title'],
			'desc'=>$post['desc'],
			'logo'=>$post['logo'],
			'textcolor'=>$post['textcolor'],
			'bgcolor'=>$post['bgcolor'],
			'is_wz'=>$post['is_wz'],
			'appid'=>$post['appid'],
			'appsecret'=>$post['appsecret'],
			'web_id'=>$post['web_id'],
			'oldid'=>$post['oldid'],
			'url'=>$post['url'],
			'shownums'=>ijson_encode($shownums),
		);
		pdo_update('core_account_baiduapp',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','',array('op'=>'list'));
	}else{
		$id=intval($_RQ['id']);
		if(!$id){
			exi('请先选择要编辑的小程序！','error');
		}
		$return['item']=pdo_get('core_account_baiduapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title','desc','logo','textcolor','bgcolor','appid','oldid','web_id','type','is_wz','url','shownums','appsecret'));
		$return['item']['shownums']=json_decode($return['item']['shownums'],true);
		$return['item']['news_num']=$return['item']['shownums']['news_num'];
		$return['item']['stores_num']=$return['item']['shownums']['stores_num'];
		$return['item']['products_num']=$return['item']['shownums']['products_num'];
		$return['item']['anlis_num']=$return['item']['shownums']['anlis_num'];
		$htmls=array(
			'type'=>'edit',
			'desc'=>'小程序',
			'tabs'=>array(
				array('op'=>'list','title'=>'小程序列表'),
			),
			'edit'=>array(
				array('type'=>'string','title'=>'小程序名称','name'=>'title' ),
				array('type'=>'string','name'=>'desc','title'=>'小程序描述','help'=>'小程序首页分享描述'),
				array('type'=>'image','name'=>'logo','title'=>'小程序LOGO','help'=>'小程序首页分享图片'),
				array('type'=>'color','name'=>'textcolor','title'=>'小程序标题颜色'),
				array('type'=>'color','name'=>'bgcolor','title'=>'小程序标题背景色','help'=>'小程序首页分享图片'),
				array('type'=>'switch','name'=>'is_wz','title'=>'第三方小程序？','help'=>'如果是第三方小程序：1、必须先设置好第三方域名；1、从第三方登陆！'),
				array('type'=>'string','name'=>'news_num','title'=>'新闻个数','help'=>'显示多少条新闻?','show'=>array('is_wz',0)),
				array('type'=>'string','name'=>'stores_num','title'=>'门店个数','help'=>'显示多少个门店?','show'=>array('is_wz',0)),
				array('type'=>'string','name'=>'products_num','title'=>'产品个数','help'=>'显示多少个产品?','show'=>array('is_wz',0)),
				array('type'=>'string','name'=>'anlis_num','title'=>'案例个数','help'=>'显示多少个案例?','show'=>array('is_wz',0)),
				array('type'=>'string','title'=>'appid','name'=>'appid','help'=>'填写小程序appid' ),
				array('type'=>'string','title'=>'appsecret','name'=>'appsecret','help'=>'填写小程序App secret'),
				//array('type'=>'select','title'=>'关联微官网','name'=>'web_id','options'=>'webs','help'=>'设置和微官网页面关联，关联后指定微官网的设计参数会直接应用到该小程序。注意，如果有多个小程序同时关联同一个微官网，则只有第一个关联会生效！' ),
				array('type'=>'link','name'=>'url','title'=>'要打包的页面链接','help'=>'如果是打包功能插件，请选择功能插件的入口地址','show'=>array('type',7)),

			),
		);
		$return['htmls']=$htmls;
		$return['webs']=pdo_getall('core_mobile_web',array('uniacid'=>$_SESSION['uniacid'],'type'=>0),array('id','title'));
		exi($return);
	}
}
if($_RQ['op']=='data'){
	//小程序数据管理：如果是微官网小程序，前往设置幻灯片和导航菜单；如果是插件小程序，前往小程序关联插件管理页
	exi('功能开发中！','error');
}
if($_RQ['op']=='change'){
	//更换小程序模版：从小程序市场中重新选择小程序模版来生成小程序（适用于插件小程序）
	exi('请选择小程序！','',array('op'=>'list','action'=>'baiduapp.plugin','query'=>'id='.$id));
}
if($_RQ['op']=='design'){
	//设计小程序：设计小程序页面，适用于diy小程序
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择小程序！','error');
		}
		$update=array(
			'items'=>$post['items']?ijson_encode($post['items']):'',
			'setting'=>$post['setting']?ijson_encode($post['setting']):''
		);
		//检测恶意代码
		if(strexists($update['items'],'<?') || strexists($update['setting'],'<?')){
			exi('提交内容存在恶意代码，无法提交！','error');
		}
		//小程序页面在发布的时候才生成
		pdo_update('core_account_baiduapp',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('保存成功！');
	}else{
		$id=intval($_RQ['id']);
		$basictype=$_RQ['basictype']?intval($_RQ['basictype']) : 0 ;
		if(!$id){
			exi('请先选择要设计的小程序！','error');
		}
		if($id){
			$_SESSION['baiduapp_id']=$id;
		}
		$return['used_navs']=array();
		$item=pdo_get('core_account_baiduapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','items','setting','title'));
		$item['items']=$item['items']?json_decode($item['items'],true):array();
		if($item['items']){
			foreach($item['items'] as $row){
				if(!$return['used_navs'][$row['nid']]){
					$return['used_navs'][$row['nid']]=pdo_get('core_mobile_blocks',array('cloud_id'=>$row['nid']),array('name','showhtml','edithtml'));
				}
			}
		}
		$item['setting']=$item['setting']?json_decode($item['setting'],true):array('bgimg'=>'');
		$navs0=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>0,'support'=>0),array(),'','usenum desc',array('10'));
		if($navs0){
			foreach($navs0 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
				
			}
		}
		$navs1=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>1,'support'=>0),array(),'','usenum desc',array('10'));
		if($navs1){
			foreach($navs1 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
				
			}
		}
		$navs2=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>2,'support'=>0),array(),'','usenum desc',array('10'));
		if($navs2){
			foreach($navs2 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
				
			}
		}
		$menus =array(
				array('title'=>'首页','iconimg'=>'../core/resource/images/diy/icon/gray_home.png','choseimg'=>'../core/resource/images/diy/icon/green_home.png','url'=>'index'),
				array('title'=>'产品','iconimg'=>'../core/resource/images/diy/icon/gray_cate.png','choseimg'=>'../core/resource/images/diy/icon/green_cate.png','url'=>'goods'),
				array('title'=>'新闻','iconimg'=>'../core/resource/images/diy/icon/gray_find.png','choseimg'=>'../core/resource/images/diy/icon/green_find.png','url'=>'news'),
				array('title'=>'我的','iconimg'=>'../core/resource/images/diy/icon/gray_people.png','choseimg'=>'../core/resource/images/diy/icon/green_people.png','url'=>'member'),
		);
		$return['navs0']=$navs0;
		$return['navs1']=$navs1;
		$return['navs2']=$navs2;
		$item['setting']['window']['navigationBarTitleText']=$item['setting']['window']['navigationBarTitleText']?$item['setting']['window']['navigationBarTitleText']:$item['title'];
		$item['setting']['window']['navigationBarBackgroundColor']=$item['setting']['window']['navigationBarBackgroundColor']?$item['setting']['window']['navigationBarBackgroundColor']:'#ff0000';
		$item['setting']['footer']['iconstyle']=$item['setting']['footer']['iconstyle']?$item['setting']['footer']['iconstyle']:0;
		$item['setting']['footer']['position']=$item['setting']['footer']['position']?$item['setting']['footer']['position']:'bottom';
		$item['setting']['footer']['menus']=!empty($item['setting']['footer']['menus']) ? $item['setting']['footer']['menus']:$menus;
		$item['setting']['bg'] = $item['setting']['bg'] ? $item['setting']['bg'] : array('bg'=>'');
		$return['item']=$item;
		$return['edit']=3;
		$return['siteroot']=get_account_url();
		$return['account']=$_SESSION['account'];
		$return['extradata']=array();
		exi($return);
	}
}
if($_RQ['op']=='getnav'){
	$params=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
	}
	$navs=pdo_getall('core_mobile_blocks',$params,array(),'','usenum desc',array('10'));
	exi($navs);
}
if($_RQ['op']=='update'){
	//更新小程序：适用于插件小程序
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要更新的小程序！','error');
	}
	$item=pdo_get('core_account_baiduapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('version','baiduapp_id'));
	if(!$item['baiduapp_id']){
		exi('出现严重错误，找不到小程序关联模版导致无法更新！可能原因是，小程序类型不是插件小程序！','error');
	}
	$new_version=pdo_getcolumn('core_baiduapp',array('id'=>$item['baiduapp_id']),'version');
	if($new_version==$item['version']){
		exi('暂无更新！','error');
	}
	pdo_update('core_account_baiduapp',array('version'=>$new_version,'status'=>0),array('id'=>$id));
	exi('更新成功！开始上传版本！','',array('op'=>'up','query'=>'id='.$id));
}
if($_RQ['op']=='result'){
	//授权结果处理
	if (empty($_RQ['auth_code'])){
		exit('授权登陆失败，请重试！');
	}
	$data=cfc('open')->get_authorization_info($_RQ['auth_code']);
	$info=cfc('open')->get_info($data['authorizer_appid']);
	
	$update=array(
		'appid'=>$data['authorizer_appid'],
		'access_token'=>$data['authorizer_access_token'],
		'expire'=>TIMESTAMP+$data['expires_in']-200,
		'refresh_token'=>$data['authorizer_refresh_token'],
		'title'=>$info['nick_name'],
		'oldid'=>$info['user_name'],
	);
	$id=intval($_RQ['id']);
	if(!$id){
		exi('授权失败！没有找到要授权的小程序，请联系管理员！','error');
	}
	pdo_update('core_account_baiduapp',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	confirm('授权成功！是否立即上传代码？',array('op'=>'up','query'=>'id='.$id),array('op'=>'list'));
}

if($_RQ['op']=='release'){
	//发布小程序
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要操作的小程序！','error');
	}
	$wxapp=cfc('open',$id);
	$wxapp->wxapp_release();
	pdo_update('core_account_baiduapp',array('status'=>4),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('发布成功！','',array('op'=>'list'));
}
if($_RQ['op']=='del'){
	//删除小程序
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_baiduapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_baiduapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='template'){
	//发布到模版市场，发布到模版市场是否需要设置价格和分类？暂时先不发布，直接保存为我的模版
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['baiduapp_id']);
		if(!$id){
			exi('请先选择小程序！','error');
		}
		$data=pdo_get('core_account_baiduapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('items','setting'));
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'group_id'=>intval($post['tygroup_idpe']),
			'title'=>$post['title'],
			'image'=>$post['image'],
			'items'=>$data['items'],
			'setting'=>$data['setting']
		);
		pdo_insert('core_baiduapp_template',$insert);
		exi('保存模版成功！');
	}else{
		$id=intval($_RQ['id']);
		if(!$id){
			exi('请先选择要保存的小程序！','error');
		}
		$return['item']=array('baiduapp_id'=>$id);
		$return['groups']=pdo_getall('core_template_group',array(),array('id','title'));
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'设置模版信息',
			'edit'=>array(
				array('type'=>'string','name'=>'title','title'=>'模版名称'),
				array('type'=>'select','name'=>'group_id','title'=>'模版类型','options'=>'groups'),
				array('type'=>'image','name'=>'image','title'=>'模版图片','help'=>'上传一张图片展示模版效果'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='up'){
	//下载应用小程序
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择小程序！');
	}
	$baiduapp=pdo_get('core_account_baiduapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$params=array(
		'siteroot'=>get_account_url(),
		'uniacid'=>$_SESSION['uniacid'],
		'baiduapp_id'=>$baiduapp['id'],
		'attachment_url'=>ATTACHMENT_URL,
		'cloud_id'=>$baiduapp['baiduapp_id'],
		'title'=>$item['title'],
		'desc'=>$item['desc'],
	);
	$res=cloud_request('wxapp.baidu_download.download',$params);
	if($res=='success'){
		exi('创建成功，请前往版本列表下载！','','/baiduapp.up_list/list');
	}
}
